home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / misc / 40 / unix.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-07-17  |  10.8 KB  |  449 lines

  1. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
  2. /* unix.c - version 1.0.3 */
  3.  
  4. /* This file collects some Unix dependencies; pager.c contains some more */
  5.  
  6. /*
  7.  * The time is used for:
  8.  *    - seed for rand()
  9.  *    - year on tombstone and yymmdd in record file
  10.  *    - phase of the moon (various monsters react to NEW_MOON or FULL_MOON)
  11.  *    - night and midnight (the undead are dangerous at midnight)
  12.  *    - determination of what files are "very old"
  13.  */
  14.  
  15. #include <stdio.h>
  16. #ifdef TOS
  17. #include <error.h>
  18. #else
  19. #include <errno.h>
  20. #endif
  21. #include "hack.h"    /* mainly for index() which depends on BSD */
  22.  
  23. #ifndef MSDOS
  24. #define MSDOS
  25. #endif
  26.  
  27. #ifdef UNIX
  28. #include    <sys/types.h>        /* for time_t and stat */
  29. #include    <sys/stat.h>
  30. #ifdef BSD
  31. #include    <sys/time.h>
  32. #else
  33. #include    <time.h>
  34. #endif BSD
  35. #endif
  36.  
  37. extern char *getenv();
  38. extern time_t time();
  39.  
  40. setrandom()
  41. {
  42.      (void) srand((int) time ((time_t *) 0));
  43. }
  44.  
  45. struct tm *
  46. getlt()
  47. {
  48.     time_t date;
  49.     struct tm *localtime();
  50.  
  51.     (void) time(&date);
  52.     return(localtime(&date));
  53. }
  54.  
  55. getyear()
  56. {
  57.     return(1900 + getlt()->tm_year);
  58. }
  59.  
  60. char *
  61. getdate()
  62. {
  63.     static char datestr[7];
  64.     register struct tm *lt = getlt();
  65.  
  66.     (void) sprintf(datestr, "%2d%2d%2d",
  67.         lt->tm_year, lt->tm_mon + 1, lt->tm_mday);
  68.     if(datestr[2] == ' ') datestr[2] = '0';
  69.     if(datestr[4] == ' ') datestr[4] = '0';
  70.     return(datestr);
  71. }
  72.  
  73. phase_of_the_moon()            /* 0-7, with 0: new, 4: full */
  74. {                    /* moon period: 29.5306 days */
  75.                     /* year: 365.2422 days */
  76.     register struct tm *lt = getlt();
  77.     register int epact, diy, golden;
  78.  
  79.     diy = lt->tm_yday;
  80.     golden = (lt->tm_year % 19) + 1;
  81.     epact = (11 * golden + 18) % 30;
  82.     if ((epact == 25 && golden > 11) || epact == 24)
  83.         epact++;
  84.  
  85.     return( (((((diy + epact) * 6) + 11) % 177) / 22) & 7 );
  86. }
  87.  
  88. night()
  89. {
  90.     register int hour = getlt()->tm_hour;
  91.  
  92.     return(hour < 6 || hour > 21);
  93. }
  94.  
  95. midnight()
  96. {
  97.     return(getlt()->tm_hour == 0);
  98. }
  99.  
  100. struct stat buf, hbuf;
  101.  
  102. gethdate(name) char *name; {
  103. #ifndef MSDOS
  104. /* old version - for people short of space */
  105. /*
  106. /* register char *np;
  107. /*    if(stat(name, &hbuf))
  108. /*        error("Cannot get status of %s.",
  109. /*            (np = rindex(name, '/')) ? np+1 : name);
  110. /*
  111. /* version using PATH from: seismo!gregc@ucsf-cgl.ARPA (Greg Couch) */
  112.  
  113.  
  114. /*
  115.  * The problem with   #include    <sys/param.h>   is that this include file
  116.  * does not exist on all systems, and moreover, that it sometimes includes
  117.  * <sys/types.h> again, so that the compiler sees these typedefs twice.
  118.  */
  119. #define        MAXPATHLEN    1024
  120.  
  121. register char *np, *path;
  122. char filename[MAXPATHLEN+1];
  123.     if (index(name, '/') != NULL || (path = getenv("PATH")) == NULL)
  124.         path = "";
  125.  
  126.     for (;;) {
  127.         if ((np = index(path, ':')) == NULL)
  128.             np = path + strlen(path);    /* point to end str */
  129.         if (np - path <= 1)            /* %% */
  130.             (void) strcpy(filename, name);
  131.         else {
  132.             (void) strncpy(filename, path, np - path);
  133.             filename[np - path] = '/';
  134.             (void) strcpy(filename + (np - path) + 1, name);
  135.         }
  136.         if (stat(filename, &hbuf) == 0)
  137.             return;
  138.         if (*np == '\0')
  139.             break;
  140.         path = np + 1;
  141.     }
  142.     error("Cannot get status of %s.",
  143.         (np = rindex(name, '/')) ? np+1 : name);
  144. #endif
  145. }
  146.  
  147. uptodate(fd) {
  148. #ifndef MSDOS
  149.     if(fstat(fd, &buf)) {
  150.         pline("Cannot get status of saved level? ");
  151.         return(0);
  152.     }
  153.     if(buf.st_mtime < hbuf.st_mtime) {
  154.         pline("Saved level is out of date. ");
  155.         return(0);
  156.     }
  157. #endif
  158.     return(1);
  159. }
  160.  
  161. #ifndef MSDOS
  162. /* see whether we should throw away this xlock file */
  163. veryold(fd) {
  164.     register int i;
  165.     time_t date;
  166.  
  167.     if(fstat(fd, &buf)) return(0);            /* cannot get status */
  168.     if(buf.st_size != sizeof(int)) return(0);    /* not an xlock file */
  169.     (void) time(&date);
  170.     if(date - buf.st_mtime < 3L*24L*60L*60L) {    /* recent */
  171.         extern int errno;
  172.         int lockedpid;    /* should be the same size as hackpid */
  173.  
  174.         if(read(fd, (char *)&lockedpid, sizeof(lockedpid)) !=
  175.             sizeof(lockedpid))
  176.             /* strange ... */
  177.             return(0);
  178.  
  179.         /* From: Rick Adams <seismo!rick>
  180.         /* This will work on 4.1cbsd, 4.2bsd and system 3? & 5.
  181.         /* It will do nothing on V7 or 4.1bsd. */
  182.         if(!(kill(lockedpid, 0) == -1 && errno == ESRCH))
  183.             return(0);
  184.     }
  185.     (void) close(fd);
  186.     for(i = 1; i <= MAXLEVEL; i++) {        /* try to remove all */
  187.         glo(i);
  188.         (void) unlink(lock);
  189.     }
  190.     glo(0);
  191.     if(unlink(lock)) return(0);            /* cannot remove it */
  192.     return(1);                    /* success! */
  193. }
  194. #endif
  195.  
  196. getlock()
  197. {
  198. #ifndef MSDOS
  199.     extern int errno, hackpid, locknum;
  200.     register int i = 0, fd;
  201.  
  202.     (void) fflush(stdout);
  203.  
  204.     /* we ignore QUIT and INT at this point */
  205.     if (link(HLOCK, LLOCK) == -1) {
  206.         register int errnosv = errno;
  207.  
  208.         perror(HLOCK);
  209.         printf("Cannot link %s to %s\n", LLOCK, HLOCK);
  210.         switch(errnosv) {
  211.         case ENOENT:
  212.             printf("Perhaps there is no (empty) file %s ?\n", HLOCK);
  213.             break;
  214.         case EACCES:
  215.             printf("It seems you don't have write permission here.\n");
  216.             break;
  217.         case EEXIST:
  218.             printf("(Try again or rm %s.)\n", LLOCK);
  219.             break;
  220.         default:
  221.             printf("I don't know what is wrong.");
  222.         }
  223.         getret();
  224.         error("");
  225.         /*NOTREACHED*/
  226.     }
  227.  
  228.     regularize(lock);
  229.     glo(0);
  230.     if(locknum > 25) locknum = 25;
  231.  
  232.     do {
  233.         if(locknum) lock[0] = 'a' + i++;
  234.  
  235.         if((fd = open(lock, 0)) == -1) {
  236.             if(errno == ENOENT) goto gotlock;    /* no such file */
  237.             perror(lock);
  238.             (void) unlink(LLOCK);
  239.             error("Cannot open %s", lock);
  240.         }
  241.  
  242.         if(veryold(fd))    /* if true, this closes fd and unlinks lock */
  243.             goto gotlock;
  244.         (void) close(fd);
  245.     } while(i < locknum);
  246.  
  247.     (void) unlink(LLOCK);
  248.     error(locknum ? "Too many hacks running now."
  249.               : "There is a game in progress under your name.");
  250. gotlock:
  251.     fd = creat(lock, FMASK);
  252.     if(unlink(LLOCK) == -1)
  253.         error("Cannot unlink %s.", LLOCK);
  254.     if(fd == -1) {
  255.         error("cannot creat lock file.");
  256.     } else {
  257.         if(write(fd, (char *) &hackpid, sizeof(hackpid))
  258.             != sizeof(hackpid)){
  259.             error("cannot write lock");
  260.         }
  261.         if(close(fd) == -1) {
  262.             error("cannot close lock");
  263.         }
  264.     }
  265. #endif
  266. }    
  267.  
  268. #ifdef MAIL
  269.  
  270. /*
  271.  * Notify user when new mail has arrived. [Idea from Merlyn Leroy, but
  272.  * I don't know the details of his implementation.]
  273.  * { Later note: he disliked my calling a general mailreader and felt that
  274.  *   hack should do the paging itself. But when I get mail, I want to put it
  275.  *   in some folder, reply, etc. - it would be unreasonable to put all these
  276.  *   functions in hack. }
  277.  * The mail daemon '2' is at present not a real monster, but only a visual
  278.  * effect. Thus, makemon() is superfluous. This might become otherwise,
  279.  * however. The motion of '2' is less restrained than usual: diagonal moves
  280.  * from a DOOR are possible. He might also use SDOOR's. Also, '2' is visible
  281.  * in a ROOM, even when you are Blind.
  282.  * Its path should be longer when you are Telepat-hic and Blind.
  283.  *
  284.  * Interesting side effects:
  285.  *    - You can get rich by sending yourself a lot of mail and selling
  286.  *      it to the shopkeeper. Unfortunately mail isn't very valuable.
  287.  *    - You might die in case '2' comes along at a critical moment during
  288.  *      a fight and delivers a scroll the weight of which causes you to
  289.  *      collapse.
  290.  *
  291.  * Possible extensions:
  292.  *    - Open the file MAIL and do fstat instead of stat for efficiency.
  293.  *      (But sh uses stat, so this cannot be too bad.)
  294.  *    - Examine the mail and produce a scroll of mail called "From somebody".
  295.  *    - Invoke MAILREADER in such a way that only this single letter is read.
  296.  *
  297.  *    - Make him lose his mail when a Nymph steals the letter.
  298.  *    - Do something to the text when the scroll is enchanted or cancelled.
  299.  */
  300. #include    "mkroom.h"
  301. static struct stat omstat,nmstat;
  302. static char *mailbox;
  303. static long laststattime;
  304.  
  305. getmailstatus() {
  306.     if(!(mailbox = getenv("MAIL")))
  307.         return;
  308.     if(stat(mailbox, &omstat)){
  309. #ifdef PERMANENT_MAILBOX
  310.         pline("Cannot get status of MAIL=%s .", mailbox);
  311.         mailbox = 0;
  312. #else
  313.         omstat.st_mtime = 0;
  314. #endif PERMANENT_MAILBOX
  315.     }
  316. }
  317.  
  318. ckmailstatus() {
  319.     if(!mailbox
  320. #ifdef MAILCKFREQ
  321.             || moves < laststattime + MAILCKFREQ
  322. #endif MAILCKFREQ
  323.                             )
  324.         return;
  325.     laststattime = moves;
  326.     if(stat(mailbox, &nmstat)){
  327. #ifdef PERMANENT_MAILBOX
  328.         pline("Cannot get status of MAIL=%s anymore.", mailbox);
  329.         mailbox = 0;
  330. #else
  331.         nmstat.st_mtime = 0;
  332. #endif PERMANENT_MAILBOX
  333.     } else if(nmstat.st_mtime > omstat.st_mtime) {
  334.         if(nmstat.st_size)
  335.             newmail();
  336.         getmailstatus();    /* might be too late ... */
  337.     }
  338. }
  339.  
  340. newmail() {
  341.     /* produce a scroll of mail */
  342.     register struct obj *obj;
  343.     register struct monst *md;
  344.     extern char plname[];
  345.     extern struct obj *mksobj(), *addinv();
  346.     extern struct monst *makemon();
  347.     extern struct permonst pm_mail_daemon;
  348.  
  349.     obj = mksobj(SCR_MAIL);
  350.     if(md = makemon(&pm_mail_daemon, u.ux, u.uy)) /* always succeeds */
  351.         mdrush(md,0);
  352.  
  353.     pline("\"Hello, %s! I have some mail for you.\"", plname);
  354.     if(md) {
  355.         if(dist(md->mx,md->my) > 2)
  356.             pline("\"Catch!\"");
  357.         more();
  358.  
  359.         /* let him disappear again */
  360.         mdrush(md,1);
  361.         mondead(md);
  362.     }
  363.  
  364.     obj = addinv(obj);
  365.     (void) identify(obj);        /* set known and do prinv() */
  366. }
  367.  
  368. /* make md run through the cave */
  369. mdrush(md,away)
  370. register struct monst *md;
  371. boolean away;
  372. {
  373.     register int uroom = inroom(u.ux, u.uy);
  374.     if(uroom >= 0) {
  375.         register int tmp = rooms[uroom].fdoor;
  376.         register int cnt = rooms[uroom].doorct;
  377.         register int fx = u.ux, fy = u.uy;
  378.         while(cnt--) {
  379.             if(dist(fx,fy) < dist(doors[tmp].x, doors[tmp].y)){
  380.                 fx = doors[tmp].x;
  381.                 fy = doors[tmp].y;
  382.             }
  383.             tmp++;
  384.         }
  385.         tmp_at(-1, md->data->mlet);    /* open call */
  386.         if(away) {    /* interchange origin and destination */
  387.             unpmon(md);
  388.             tmp = fx; fx = md->mx; md->mx = tmp;
  389.             tmp = fy; fy = md->my; md->my = tmp;
  390.         }
  391.         while(fx != md->mx || fy != md->my) {
  392.             register int dx,dy,nfx = fx,nfy = fy,d1,d2;
  393.  
  394.             tmp_at(fx,fy);
  395.             d1 = DIST(fx,fy,md->mx,md->my);
  396.             for(dx = -1; dx <= 1; dx++) for(dy = -1; dy <= 1; dy++)
  397.                 if(dx || dy) {
  398.                 d2 = DIST(fx+dx,fy+dy,md->mx,md->my);
  399.                 if(d2 < d1) {
  400.                     d1 = d2;
  401.                     nfx = fx+dx;
  402.                     nfy = fy+dy;
  403.                 }
  404.                 }
  405.             if(nfx != fx || nfy != fy) {
  406.                 fx = nfx;
  407.                 fy = nfy;
  408.             } else {
  409.                 if(!away) {
  410.                 md->mx = fx;
  411.                 md->my = fy;
  412.                 }
  413.                 break;
  414.             } 
  415.         }
  416.         tmp_at(-1,-1);            /* close call */
  417.     }
  418.     if(!away)
  419.         pmon(md);
  420. }
  421.  
  422. readmail() {
  423. #ifdef DEF_MAILREADER            /* This implies that UNIX is defined */
  424.     register char *mr = 0;
  425.     more();
  426.     if(!(mr = getenv("MAILREADER")))
  427.         mr = DEF_MAILREADER;
  428.     if(child(1)){
  429.         execl(mr, mr, (char *) 0);
  430.         exit(1);
  431.     }
  432. #else DEF_MAILREADER
  433.     (void) page_file(mailbox, FALSE);
  434. #endif DEF_MAILREADER
  435.     /* get new stat; not entirely correct: there is a small time
  436.        window where we do not see new mail */
  437.     getmailstatus();
  438. }
  439. #endif MAIL
  440.  
  441. regularize(s)    /* normalize file name - we don't like ..'s or /'s */
  442. register char *s;
  443. {
  444.     register char *lp;
  445.  
  446.     while((lp = index(s, '.')) || (lp = index(s, '/')))
  447.         *lp = '_';
  448. }
  449.